package TUILayer;//makes this class a part of the TUILayer package
import java.util.Scanner;
import ControlLayer.*;//The TUILayer needs to know the ControlLayer
 

/**
 * User interface class to the AddressBook
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * (FEN): 2006.03.21: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class AddressBookUI
{   
    private AddressCtr addrCtr;

    
    //constructor.
    public AddressBookUI()
    {
       addrCtr = addrCtr.getInstance();
    }
    
    public void start()
    {
        //Starts the address book menu.
        addressMenu();
    }
    
    public void addressMenu()
    {
       boolean exit = false;
       while(!exit)//that is: while exit is false
       {
          int choice = writeAddressBookMenu(); 
          switch(choice)
          {
              case 1:
                getPerson();
                break;
              case 2:
                createPerson();
                break;
              case 3:
                 deletePerson();
                 break;
              case 4:
                 updatePerson();
                 break;
              case 5:
                 listAllPersons();
                 break;
              case 6:
              
              default:
                 exit = true;
                 break;
          }
       }//end while
    }
    
    //Updates a person's information.
    public void updatePerson()
    {   Scanner keyboard = new Scanner(System.in);
        System.out.println("Write ID: ");
        long _id=keyboard.nextInt();
         addrCtr.p=addrCtr.getPerson(_id);

        String value;
        int val;
        boolean exit = false;
        while(!exit) //that is: while exit is false
        {
            int choice = changeInfo(); 
           switch(choice)
           {    case 1:
                     val= keyboard.nextInt();
                     addrCtr.p.setID(val);
                    break;
                case 2:
                    value=keyboard.next();
                    addrCtr.p.setname(value);
                    break;
                case 3:
                    value=keyboard.next();
                    addrCtr.p.setaddress(value);
                    break;
                case 4:
                    value=keyboard.next();
                    addrCtr.p.setPostnr(value);
                    break;
                case 5:
                    value=keyboard.next();
                    addrCtr.p.setcity(value);
                    break;
                case 6:
                    value=keyboard.next();
                    addrCtr.p.settel(value);
                    break;
                case 7:
                    // Do nothing go to default
                default:
                    exit = true;
                    break;
           }
        } //end while.
    }
    
    public int changeInfo()
    {
        //The menu for updating information.
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" (1) Change ID");
        System.out.println(" (2) Change name");
        System.out.println(" (3) Change address");
        System.out.println(" (4) Change post code");
        System.out.println(" (5) Change city");
        System.out.println(" (6) Change telephone number");
        System.out.println(" (7) Finish changes");
        System.out.print("\n Make your choice: ");
        int choice = keyboard.nextInt();
        return choice;
    }
    
    public int writeAddressBookMenu()
    {      // makes an object keyboard to read input from the screen
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** AddressBook Menu ***");
            System.out.println(" (1) Show information about person");
            System.out.println(" (2) Add person");
            System.out.println(" (3) Delete person");
            System.out.println(" (4) Update person");
            System.out.println(" (5) List all persons");
            System.out.println(" (6) Return");
            System.out.print("\n Make your choice: ");
            int choice = keyboard.nextInt();
            return choice;
    }
        
    //the method reads id for a person and return
    public long inputPersonsID()
    {   
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Id:  ");
           try
           {
             id = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e)
           {
             System.out.println("You must enter a number -  try again");
             String input = keyboard.nextLine();
           }
        }//end while
        return id;
    }
       
    public String inputName()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Name:  ");
         String name = keyboard.nextLine();
         return name;
    }
   
    public String inputPostalCode()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Postal Code:  ");
         String code = keyboard.nextLine();
         return code;
    }
    public String inputCity()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write City:  ");
         String city = keyboard.nextLine();
         return city;
    } 
    public String inputPhone()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Phone Number:  ");
         String phone = keyboard.nextLine();
         return phone;
    } 
     
    public String  inputAddress()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write Address: ");
        String address = keyboard.nextLine();
        return address;
    }
    
     private void createPerson()
    {
        //Adds a person to the address book.
        long id = inputPersonsID();
        String name = inputName();
        String address = inputAddress();
        String postalCode = inputPostalCode();
        String city = inputCity();
        String phone = inputPhone();
        addrCtr.createPerson(id, name, address, postalCode,city, phone);
    }
    
    public void deletePerson()
    {
        //Removes a person from the address book.
        long id=inputPersonsID();
        addrCtr.deletePerson(id);
    }
    
    //Prints the id/ name/ address of the person.
    public void showId(long _id)
    {
        System.out.println("ID of the person: " + _id);
    }
    public void showName(String _name)
    { 
        System.out.println("The name of the persone: " + _name);
    }
    public void showAddress(String _address)
    { 
        System.out.println("The address of the person: " + _address);
    }

    public void getPerson()
    {   //is to be changed when the address book controller is completed
        long id = inputPersonsID();
        String str = addrCtr.printPerson(addrCtr.getPerson(id));
        System.out.print(str);
        Scanner keyboard = new Scanner(System.in);
        String input = keyboard.nextLine();
    }
  
    private void listAllPersons()
    {
        //Prints a list with every person in the address book.
       String str=addrCtr.listAllPersons();
       System.out.println(str);
       Scanner keyboard = new Scanner(System.in); 
       keyboard.nextLine();
    }
}
            
        
